﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Caching;

namespace PJ.Data.SalePurchase
{
    /// <summary>
    /// ShoppingCartItemPICProvider : Persist In Cache data provider
    /// </summary>
    /// <typeparam name="I">Object to provide interface type</typeparam>
    /// <typeparam name="C">Object to provide concrete class type</typeparam>
    public abstract class ShoppingCartItemPICProvider<I, C> : ObjectPICProvider<I, C>, IShoppingCartItemProvider<I>
        where C : BaseShoppingCartItemData, I, new()
        where I : class, IShoppingCartItemData
    {
        public ShoppingCartItemPICProvider(ICacheManager cache)
            : base(cache)
        {
        }


        public I GetByOrder(string parentId, double order, GetDataMode mode)
        {
            return OrderedFragmentPICProvider<C>.GetByOrder(DataObjects, parentId, order, mode);
        }

        public string[] GetFragments(string ownerId, GetDataMode mode)
        {
            return EntityFragmentPICProvider<C>.GetFragments(DataObjects, ownerId, mode);
        }

        public double Up(string parentId, double order)
        {
            return OrderedFragmentPICProvider<C>.Up(DataObjects, parentId, order);
        }

        public double Down(string parentId, double order)
        {
            return OrderedFragmentPICProvider<C>.Down(DataObjects, parentId, order);
        }

        public bool IsFirst(string parentId, double order)
        {
            return OrderedFragmentPICProvider<C>.IsFirst(DataObjects, parentId, order);
        }

        public bool IsLast(string parentId, double order)
        {
            return OrderedFragmentPICProvider<C>.IsLast(DataObjects, parentId, order);
        }

        public bool OrderExists(string parentId, double order)
        {
            return OrderedFragmentPICProvider<C>.OrderExists(DataObjects, parentId, order);
        }

        IOrderedFragmentData IOrderedFragmentProvider.GetByOrder(string parentId, double order, GetDataMode mode)
        {
            return GetByOrder(parentId, order, mode);
        }

        public double NextOrder(string ownerId)
        {
            var ownerItems = (from p in DataObjects.Values where p.OwnerId == ownerId select p.Order);
            if (ownerItems.Count() == 0)
                return 10;
            else
                return ownerItems.Max() + 10;
        }
    }
}
